﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoreProvince;
using CoreNhaDat;

namespace BatDongSan.Ucs
{
    public partial class SearchBox : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string _keyWord;
            int _loaiNhaDatId;
            int _nhaDatId;
            int _provinceId;
            int _wardId;
            int _price;
            int _area;
            try
            {
                try
                {
                    _keyWord = Request.QueryString["key"];
                }
                catch
                {
                    _keyWord = "";
                }
                _price = Convert.ToInt32(Request.QueryString["gia"]);
                _area = Convert.ToInt32(Request.QueryString["dt"]);
                _loaiNhaDatId = Convert.ToInt32(Request.QueryString["loai"]);
                _nhaDatId = Convert.ToInt32(Request.QueryString["nhadat"]);
                _provinceId = Convert.ToInt32(Request.QueryString["tp"]);
                _wardId = Convert.ToInt32(Request.QueryString["quan"]);
                string gia = "";
                if (_price > 0)
                    gia = GiaBll.GetById(_price, Utility.strConnect).Name;
                string dt = "";
                if (_area > 0)
                {
                    dt = DienTichBll.GetById(_area, Utility.strConnect).Name;
                }
                
                if (!IsPostBack)
                {
                    BindingDdlProvince();
                    ddlProvince.SelectedValue = _provinceId.ToString();
                    BindingDdlWard(Convert.ToInt32(ddlProvince.SelectedValue));
                    ddlWard.SelectedValue = _wardId.ToString();

                    BindingNhaDat();
                    ddlNhaDat.SelectedValue = Convert.ToString(_nhaDatId);
                    BindingLoaiNhaDat(_nhaDatId);
                    ddlLoaiNhaDat.SelectedValue = _loaiNhaDatId.ToString();
                    BindingDdlPrice();
                    ddlPrice.SelectedValue = _price.ToString();
                }
            }
            catch { }
        }
        protected void BindingDdlProvince()
        {
            try
            {
                ddlProvince.DataSource = ProvinceBll.GetList(Utility.strConnect);
                ddlProvince.DataTextField = "Name";
                ddlProvince.DataValueField = "Id";
                ddlProvince.DataBind();
                ListItem li1 = new ListItem("Chọn Tỉnh / Thành", "0");
                ddlProvince.Items.Insert(0, li1);

            }
            catch { }
        }

        protected void BindingDdlWard(int idProvince)
        {
            try
            {
                    ddlWard.DataSource = WardBll.GetList(false, true, idProvince, Utility.strConnect);
                    ddlWard.DataTextField = "Name";
                    ddlWard.DataValueField = "Id";
                    ddlWard.DataBind();
                    ListItem li1 = new ListItem("Chọn Quận / Huyện", "0");
                    ddlWard.Items.Insert(0, li1);
            }
            catch { }
        }

        protected void BindingNhaDat()
        {
            try
            {
                ddlNhaDat.DataSource = NhaDatBll.GetList(false, true, Utility.strConnect);
                ddlNhaDat.DataTextField = "Name";
                ddlNhaDat.DataValueField = "Id";
                ddlNhaDat.DataBind();
                ListItem li1 = new ListItem("Chọn loại tin", "0");
                ddlNhaDat.Items.Insert(0, li1);
            }
            catch { }
        }
        
        protected void BindingLoaiNhaDat( int nhaDatId)
        {

            try
            {
                ddlLoaiNhaDat.DataSource = LoaiNhaDatBll.GetList(false, true, nhaDatId, Utility.strConnect);
                ddlLoaiNhaDat.DataTextField = "Name";
                ddlLoaiNhaDat.DataValueField = "Id";
                ddlLoaiNhaDat.DataBind();
                ListItem li1 = new ListItem("Chọn loại nhà đất", "0");
                ddlLoaiNhaDat.Items.Insert(0, li1);
            }
            catch { }
        }

        protected void BindingDdlPrice()
        {
            try
            {
                ddlPrice.DataSource = GiaBll.GetList(false, true, Utility.strConnect);
                ddlPrice.DataTextField = "Name";
                ddlPrice.DataValueField = "GiaId";
                ddlPrice.DataBind();
                ListItem li1 = new ListItem("Chọn mức giá", "0");
                ddlPrice.Items.Insert(0, li1);
                
            }
            catch { }
        }

        protected void ddlNhaDat_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindingLoaiNhaDat(Convert.ToInt32(ddlNhaDat.SelectedValue));
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindingDdlWard(Convert.ToInt32(ddlProvince.SelectedValue));
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            string re = "";
            
            re += "?gia="+ddlPrice.SelectedValue;
            re += "&nhadat=" + ddlNhaDat.SelectedValue;
            re += "&loai=" + ddlLoaiNhaDat.SelectedValue;
            re += "&tp=" + ddlProvince.SelectedValue;
            re += "&quan=" + ddlWard.SelectedValue;
            if (!string.IsNullOrEmpty(txtTuKhoa.Text.Trim()))
            {
                re += "&key=" + txtTuKhoa.Text.Trim();
            }
            string link = "/Search.aspx"+re;
            Response.Redirect(link);
        }
    }
}